<template>
  <view class="item_block_pu">
    <view class="network">
      <view class="left">
        <nut-checkbox @change="changeCheckBranch" v-model="props.item.check" label="" style=""></nut-checkbox>
        <image src="../../assets/images/codeList/network_house.png"></image>
        <text class="branchText">{{ props.item.companyName }}网点</text>
      </view>
      <view class="right">
        <image src="../../assets/images/codeList/phone.png" @touchStart="toDial(props.item.tel)"></image>
        <image src="../../assets/images/codeList/marker.png" style="margin-left: 32rpx" @touchStart="toMap(props.item)"></image>
      </view>
    </view>
    <view class="codes" v-for="(item,index) in props.item.orderList">
      <view class="order">
        <view class="order_text">
          <nut-checkbox v-model="item.check" label=""></nut-checkbox>
          <text style="margin-left:8rpx">承运单号：{{ item.billNo }}</text>
          <image src="../../assets/images/codeList/paper.png" @tap="copyCode(item.billNo)"></image>
        </view>
        <view class="order_btn" @click="toOrderDetail(item)">订单详情＞</view>
      </view>
      <view class="addr">
        <view class="title_1">发</view>
        <view class="name">{{ item.shipper }}</view>
        <view class="pos">{{ item.toProvince }}&nbsp;{{ item.toFromCity }}</view>
      </view>
      <view class="addr">
        <view class="title_2">收</view>
        <view class="name">{{ item.receiver }}</view>
        <view class="pos">{{ item.fromProvince }}&nbsp;{{ item.fromCity }}</view>
      </view>
      <view class="goods">
        货物信息：{{goodsInfoString(item)}}
      </view>
      <view class="tools">
        <view class="left" v-if="item.allPaid">已结清 ¥{{parseFloat(item.paidMoney||0).toFixed(2)}}</view>
        <view class="left" v-else style="color:#ff8f1f">待付款 ¥{{parseFloat(getLeftPrice(item)).toFixed(2)}}</view>
        <view class="right">
          <view class="gengduo" @click="showMore(index)">
            <text class="gengduo_text">更多</text>
            <IconFont name="rect-down" size="24rpx" color="#999" v-if="!showMenu[index]"></IconFont>
            <IconFont name="rect-up" size="24rpx" color="#999" v-if="showMenu[index]"></IconFont>
            <view class="more_menu" v-show="showMenu[index]">
              <view class="menu_row" @click.stop="gotoCallSome(item,index)">找人代取</view>
              <view class="menu_row" @click.stop="gotoForwarding(item,index)">转代送</view>
            </view>
          </view>
          <view class="quhuo" @click="pickupOne(item)">办理取货</view>
        </view>
      </view>
    </view>

    <dialBranch :show="showTelPopup" :tel="branchTel" :title="'拨打电话'" @close="showTelPopup=false" :show-close="true"></dialBranch>
  </view>
</template>

<script setup lang="ts">
import Taro, {useDidShow} from "@tarojs/taro"
import { IconFont } from '@nutui/icons-vue-taro';
import { ref, defineProps, defineEmits,onMounted,} from 'vue'
import {Del, Edit} from "@nutui/icons-vue-taro";
import {$get,$post} from "@/@api";
import useMyOrderStore from "@/store/myOrder";
import useUserInfoStore from "@/store/user";
import useTransactionStore,{ transaction } from "@/store/pickUp";
import dialBranch from "@/components/dialBranch/index"
import { nextTick } from "@tarojs/runtime";
const props = defineProps<{
  item: any
  index: number,
}>()
const emit = defineEmits(['gotoCallSome','gotoForwarding','chooseAll','pickUpOne'])

const orderStore = useMyOrderStore()
const userStore = useUserInfoStore()
const transactionStore = useTransactionStore()

//显示更多菜单
const showMenu = ref<boolean[]>([])

const copyCode = (val)=>{
  Taro.setClipboardData({
    data: val
  })
}

//计算代付款金额
const getLeftPrice = (row)=>{
  return row.costList.reduce((sum,item)=>{
    return sum + (item.actualMoney - item.paidMoney)
  },0)
}

//网点电话弹窗
const showTelPopup = ref<boolean>(false)
const branchTel = ref<string>('')
const toDial= async (tel) => {
  if(tel){
    branchTel.value = tel
    showTelPopup.value = true
  }
  else{
    Taro.showToast({
      icon: 'none',
      title: '该网点无联系方式'
    })
  }
}

//打开更多弹窗
const showMore = (indexing)=>{
  
  showMenu.value.forEach((item,index)=>{
    index==indexing?showMenu.value[indexing] = !showMenu.value[indexing]:showMenu.value[index]=false
  })
  console.log(indexing,props.item,showMenu.value);
}

const toMap = (item:any) => {
  Taro.openLocation({
    longitude: Number(item.localX || item.lng),
    latitude: Number( item.localY || item.lat),
    scale: 11
  })
}

const toOrderDetail = async (item:any) => {
  let res: any = await $post(
      //"/selfmachine-system/workerOrder/orderList",
      "/selfmachine-system/order/list",
      {
        billNo: item.billNo,
      }
  )
  orderStore.orderList = res.data.rows[0]
  Taro.navigateTo({
    url: "/pages/myOrder/order",
  });
}

const goodsInfoString = (item)=>{
  let goods = ""
  item.goodsInfo.forEach((it) => {
    goods =
        goods +
        it.goodsName +
        it.goodsNumber +
        "件," +
        it.weigh +
        "Kg," +
        it.goodsVolume +
        "m³," +
        it.packing +
        ";";
  })
  return goods;
}

//单个取货
function pickupOne(item: any) {
  let list: any = []
  list.push(item)
  //旧版
  // transaction({ bills: list, customerCode: userStore.userInfo.customerCode })
  //新版
  transactionStore.transactionList = list
  emit('pickUpOne')
}
//单个转代送
const gotoForwarding = (item: any, index: number) => {
  emit('gotoForwarding',{
    index: props.index,
    orderIndex: index
  })
  // let aa: any = [];
  // aa.push(item)
  // transactionStore.transactionList = aa;
  // Taro.navigateTo({
  //   url: "/pages/pickUp/forwarding",
  // })
}
//单个找人代取
const gotoCallSome = (item: any, index: number)=>{
  emit('gotoCallSome',{
    index: props.index,
    orderIndex: index
  })
}

//网点前的选中按钮
const changeCheckBranch = ()=>{
  if(props.item.check){
    props.item.orderList.forEach(item=>{
      item.check = true
    })
  }
  else{
    props.item.orderList.forEach(item=>{
      item.check = false
    })
  }
}
onMounted(()=>{
  props.item.orderList.forEach((element,index) => {
    showMenu.value[index]=false
  });
})
useDidShow(()=>{
    props.item.orderList.forEach((element,index) => {
    showMenu.value[index]=false
  }); 
})

</script>

<style lang="scss">
.item_block_pu {
  width: 100%;
  background-color: #fff;
  border-radius: 16rpx;
  margin-top: 16rpx;
  overflow:visible;
  padding: 0 24rpx;
  box-sizing: border-box;
  .network {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 104rpx;
    border-bottom: 1px solid #f5f5f5;
    .left,.right {
      display: flex;
      align-items: center;
      image {
        width: 36rpx;
        height: 36rpx;
        margin-left: 8rpx;
      }
      .branchText {
        margin-left: 8rpx;
        font-size: 28rpx;
        color: #252525;
        font-weight: 500;
      }
    }
  }
  .codes {
    width: 100%;
    box-sizing: border-box;
    padding-bottom: 24rpx;
    .order {
      width: 100%;
      height: 28rpx;
      margin-top: 24rpx;
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 22rpx;
      color: #252525;
      .order_text {
        display: flex;
        align-items: center;
        image {
          width: 18rpx;
          height: 20rpx;
          margin-left: 3rpx;
        }
      }
      .order_btn {
        color: #999;
      }
    }
  }
  .addr {
    width: calc(100% - 48rpx);
    margin-left: 48rpx;
    margin-top: 24rpx;
    display: flex;
    align-items: center;
    font-weight: 500;
    .title_1 {
      width: 40rpx;
      height: 40rpx;
      line-height: 40rpx;
      overflow:ellipsis;
      text-align: center;
      background: linear-gradient(150deg, #3783FF 14%, #125AD2 93%);
      color: #fff;
      font-size: 20rpx;
      border-radius: 8rpx;
    }
    .title_2 {
      width: 40rpx;
      height: 40rpx;
      overflow:ellipsis;
      line-height: 40rpx;
      text-align: center;
      background: linear-gradient(137deg, #13E879 14%, #0EC365 89%);
      color: #fff;
      font-size: 20rpx;
      border-radius: 8rpx;
    }
    .name {
      margin-left: 16rpx;
      font-size: 28rpx;
      color: #252525;
      width: 120rpx;
      overflow:ellipsis;
      white-space: nowrap;
      font-weight: bold;
    }
    .pos {
      margin-left: 24rpx;
      width: calc(100% - 200rpx);
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
      color: #252525;
      font-size: 28rpx;
      font-weight: bold;
    }
  }
  .goods {
    width: calc(100% - 48rpx);
    margin-left: 48rpx;
    border-radius: 8rpx;
    background-color: #f5f5f5;
    margin-top: 24rpx;
    font-size: 24rpx;
    line-height: 32rpx;
    color: #666;
    box-sizing: border-box;
    padding: 24rpx;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
  .tools {
    width: calc(100% - 48rpx);
    margin-left: 48rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 24rpx;
    .left {
      font-size: 28rpx;
      color: #252525;
      font-weight: bold;
    }
    .right {
      display: flex;
      align-items: center;
      .gengduo {
        width: 144rpx;
        height: 56rpx;
        font-size: 24rpx;
        border-radius: 28rpx;
        box-sizing: border-box;
        display: flex;
        justify-content: center;
        align-items: center;
        border: 1rpx solid #c4c4c4;
        color: #252525;
        position: relative;
        .gengduo_text {
          margin-right: 8rpx;
        }
        .more_menu {
          position: absolute;
          left: 0;
          top: 72rpx;
          width: 252rpx;
          border-radius: 16rpx;
          background: #fff;
          box-shadow: 0 6rpx 16rpx 0 rgba(0, 0, 0, 0.16);
          box-sizing: border-box;
          padding: 0 24rpx;
          .menu_row {
            width: 100%;
            height: 72rpx;
            line-height: 72rpx;
            font-size: 24rpx;
            color: #252525;
            &:first-child {
              border-bottom: 1rpx solid #f2f2f2;
            }
          }
        }
      }
      .quhuo {
        width: 144rpx;
        height: 56rpx;
        font-size: 24rpx;
        border-radius: 28rpx;
        box-sizing: border-box;
        display: flex;
        justify-content: center;
        align-items: center;
        border: 1rpx solid #0966ff;
        color: #0966ff;
        margin-left: 16rpx;
      }
    }
  }
}
.nut-checkbox__label {
  margin-left: 0;
}
</style>
